<?php
namespace app\bancard\controller;
use think\Db;
use think\facade\Cache;
use app\common\lib\Curl;
use app\common\validate\NoticeValidate;
use app\bancard\model\Notice as NoticeModel;
class Notice extends Common {
	
	//推送key;
    protected $pushKey = [
        'action' => 'refresh',
        'params' => 'notice',
    ];
	//首页
	public function index(){
		return $this->fetch();
	}
    public function lists(){
        $user_id = session('admin.id');
        $school_id = session('schoolInfo.id');
        $ntitle =request()->param('keywords');
        $title = isset($ntitle)?$ntitle:'';
        $limit = request()->param('limit');
        $offset = request()->param('offset');
        $start = ($offset-1)*$limit;
        $map =[
            'user_id'=>$user_id,
            'school_id'=>$school_id
        ];
        if($title){
            $count = db('notice')->where($map)->whereLike('title',"%$title%")->count();
            $result = db('notice')->where($map)->whereLike('title',"%$title%")->limit($start,$limit)->order('sort desc,id desc')->select();
        }else{
            $count = db('notice')->where($map)->count();
            $result = db('notice')->where($map)->limit($start,$limit)->order('sort desc,id desc')->select();
        }
        return showTable(0, 'OK', $count ,$result, 200);
    }
	//查看
	public function view(){
		$data = request()->param();
		$result = db('notice')->where('id',$data['id'])->find();
		$beforePage = $this->beforePage($data);
		$afterPage = $this->afterPage($data);
		$this->assign('beforePage',$beforePage);
		$this->assign('afterPage',$afterPage);
		$this->assign('result',$result);
		return $this->fetch();
	}
	
	//下一篇
	public function beforePage($data){
		$id = request()->param('id');
		$result = db('notice')->where('id','>',$id)->limit(1)->find();
		if(empty($result)){
			$result   = "上一篇:<a href='javascript:;'>没有了</a>";
		}else{
			$url = url('bancard/Notice/view',['id'=>$result['id']]);
			$result = "上一篇:<a href='".$url."' class='before'>".mb_substr($result['title'],0,20,'utf-8')."</a>";
		}
		return $result;
	}
	//上一篇
	public function afterPage($data){
		$id = request()->param('id');
		$result = db('notice')->where('id','<',$id)->limit(1)->order('id','desc')->find();
		
		if(empty($result)){
			$result   = "下一篇:<a href='javascript:;'>没有了</a>";
		}else{
			$url = url('bancard/Notice/view',['id'=>$result['id']]);
			$result = "下一篇:<a href='".$url."' class='after'>".mb_substr($result['title'],0,20,'utf-8')."</a>";
		}
		return $result;
	}
	//编辑
	public function edit(){
		if(request()->isPost()){
			$data = request()->param();
			$data['user_id'] = session('admin.id');
			$data['school_id'] = session('schoolInfo.id');
			$data['platform'] = 1;
			$data['class_ids'] =  implode(",", $data['class_ids']);
			$validate = new NoticeValidate;
			if (!$validate->scene('edit')->check($data)) {
				return show(0, $validate->getError(), 0 , 200);
			}else{
				Cache::clear('notice');
				$rest = (new NoticeModel())->strict(false)->where('id',$data['id'])->update($data);
				if($rest){
					//如果发布信息成功推送到相对于的设备上进行更新相对应的模块后期加入log日志判断是否发送成功
					$this->socketPush($data['class_ids'],$this->pushKey);
				}
				return show(1, '修改成功', $rest, 200);
			}			
		}
		$data = request()->param();
		$result = db('notice')->where('id','=',$data['id'])->find();
		$egetClass = $this->egetClass($result['class_ids']);
		$this->assign('egetClass', json_encode($egetClass['data']));
		$this->assign('result',$result);
		return $this->fetch();
	}
	//添加
	public function add(){
		
		if(request()->isPost()){
			$data = request()->param();
			$data['created_at'] = date('Y-m-d H:i:s', time());
			$data['user_id'] = session('admin.id');
			$data['school_id'] = session('schoolInfo.id');
			$data['platform'] = 1;
			$data['class_ids'] =  implode(",", $data['class_ids']);
			$validate = new NoticeValidate;
			if (!$validate->scene('add')->check($data)) {
				return show(0, $validate->getError(), 0 , 200);
			}else{
				Cache::clear('notice');
				$rest = (new NoticeModel())->strict(false)->insert($data);
				if($rest){
					//如果发布信息成功推送到相对于的设备上进行更新相对应的模块后期加入log日志判断是否发送成功
					$this->socketPush($data['class_ids'],$this->pushKey);
				}
				return show(1, '添加成功', $rest, 200);
			}			
		}
		$egetClass = $this->getClass();
		$this->assign('egetClass', json_encode($egetClass['data']));
		return $this->fetch();
	}
	//批量删除
	public function delete(){
		Cache::clear('notice');
		$data = request()->param();
        $newInfo = db('notice')->where(['id'=>$data['ids']])->where(['user_id'=>$data['user_id']])->select();
        $result = array_column($newInfo,'class_ids');
        $result = implode(',', $result);
        $result = explode(',', $result);
        $class_ids = array_unique($result);
        $this->socketPush($class_ids,$this->pushKey);
		$result = db('notice')->where(['user_id'=>$data['user_id']])->delete($data['ids']);
		return show(1, '删除成功', $result, 200);
	}
	//是否显示
	public function show(){
		$data = request()->param();
		Cache::clear('notice');

		$result = db('notice')->where('id',$data['id'])->update(['show'=>$data['status']]);
        $newInfo = db('notice')->where(['id'=>$data['id']])->find();
        $class_ids = explode(',', $newInfo['class_ids']);
        $this->socketPush($class_ids,$this->pushKey);
		return show(1, '设置成功', $result, 200);
	}
	//是否置顶
	public function top(){
		$data = request()->param();
		Cache::clear('notice');
		$result = db('notice')->where('id',$data['id'])->update(['istop'=>$data['status']]);
        $newInfo = db('notice')->where(['id'=>$data['id']])->find();
        $class_ids = explode(',', $newInfo['class_ids']);
        $this->socketPush($class_ids,$this->pushKey);
		return show(1, '设置成功', $result, 200);
	}
	//排序
	public function sort(){
		$data = request()->param();
		
		$validate = new NoticeValidate;
		
		if (!$validate->scene('sort')->check($data)) {
			return show(config('code.error'), 'error', $validate->getError(), 200);
		}else{
			Cache::clear('notice');
			$rest = (new NoticeModel())->save(['sort'=>$data['sort']],['id'=>$data['id']]);
			return show(config('code.success'), 'OK', $rest, 200);
		}
			
	}
}